<?php

namespace app\stuff\model;

/**
 * Created by PhpStorm.
 * User: Xyzhao
 * Date: 2017/7/18
 * Time: 11:52
 */

use think\Model;
use think\session;

class Users extends Model
{
	public function checkUser($CSUid, $password)
	{
		$where = array("CSUid" => $CSUid);
		$res = $this->where($where)->select();
		if (!empty($res)) {
			$hash = $res[0]['password'];
			return password_verify($password, $hash) ? $res[0] : false;
		}
		return false;
	}

	public function checkSession($name, $CSUid)
	{
		$where = array('name' => $name, 'CSUid' => $CSUid);
		return $this->where($where)->count() ? true : false;
	}

	public function self()
	{
		$res = false;
		if (Session::has('name') && Session::has('CSUid')) {
			$res = $this->query('SELECT * FROM `users`,`authority` WHERE `CSUid`=? AND `name`=?;',
				[Session::get('CSUid'), Session::get('name')])[0];
			unset($res['password']);
			unset($res['phone']);
			unset($res['email']);
			unset($res['qq']);
			unset($res['openid']);
			$res['authority'] = json_decode($res['authority']);
		}
		return $res;
	}

	public function users()
	{
		$res = $this->query('SELECT * FROM `users`,`authority` WHERE users.id=authority.auth_id;');
//		dump($res);
		$user = [];
		foreach ($res as $value) {
			unset($value['password'],$value['auth_id']);
			$value['authority'] = json_decode($value['authority']);
			$user[] = $value;
		}
		return $user;
	}
}